Linux查看java进程的端口lsof以及netstat查看进程和端口号相关命令

操作系统:Centos6.4 64位

在linux操作时,经常要查看运行的项目的进程和端口号,在这里总结了以下常用到的相关命令:

1.查看系统运行的java项目,并查看进程号

这个用到的命令为:

1
ps -ef|grep java
1
2
[root@localhost ~]# ps -ef|grep java
root 26517 26496 0 08:52 pts/0 00:00:00 grep --color=auto java

2.lsof命令:根据进程pid查端口:

1
lsof -i | grep pid
1
2
3
4
[root@localhost bty-bty]# lsof -i | grep 26541
java 26541 root 25u IPv6 192402 0t0 TCP localhost:38300->localhost:ms-sql-s (ESTABLISHED)
java 26541 root 27u IPv6 189113 0t0 TCP localhost:38304->localhost:ms-sql-s (ESTABLISHED)
java 26541 root 28u IPv6 192661 0t0 TCP localhost:38308->localhost:ms-sql-s (ESTABLISHED)

其中,java表示项目运行类型,26541表示进程号,root表示用户名

3.根据端口port查进程:

1
lsof -i:port
1
2
3
[root@localhost bty-bty]# lsof -i:82
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 26541 root 51u IPv6 192420 0t0 TCP *:xfer (LISTEN)

pid对应的为进程号,root为用户名,

4.根据用户查看进程和端口号:

1
lsof -i|grep user
1
2
3
4
5
6
[root@localhost bty-bty]#  lsof -i|grep root
dhclient 8966 root 6u IPv4 51387 0t0 UDP *:bootpc
sshd 9163 root 3u IPv4 53558 0t0 TCP *:ssh (LISTEN)
sshd 9163 root 4u IPv6 53560 0t0 TCP *:ssh (LISTEN)
master 9390 root 13u IPv4 51156 0t0 TCP localhost:smtp (LISTEN)
master 9390 root 14u IPv6 51157 0t0 TCP localhost:smtp (LISTEN)

其查询结果同lsof -i | grep pid,可参见第2个结果。

5. netstat命令根据进程pid查端口:

1
netstat -nap | grep pid
1
2
3
[root@localhost bty-bty]#  netstat -nap | grep 26541
tcp6 0 0 :::82 :::* LISTEN 26541/java
tcp6 0 0 192.168.244.128:48734 101.133.200.106:1433 ESTABLISHED 26541/java

6.根据端口port查进程

1
netstat -nap | grep port
1
2
3
4
5
[root@localhost bty-bty]#  netstat -nap | grep 82
tcp6 0 0 :::82 :::* LISTEN 26541/java
unix 2 [ ACC ] STREAM LISTENING 61076 13516/containerd-sh /run/containerd/s/98264f6aebf6620456f96edaa2389ae4c5a247e0e9085d298cc69bbc0b24a0dd
unix 3 [ ] STREAM CONNECTED 51182 9390/master
unix 3 [ ] STREAM CONNECTED 62809 13516/containerd-sh /run/containerd/s/98264f6aebf6620456f96edaa2389ae4c5a247e0e9085d298cc69bbc0b24a0dd
继开 wechat
欢迎加我的微信,共同交流技术